#include "GroupSessionDetailDialog.h"
#include <QGridLayout>
#include <QScrollArea>
#include "Custom/CircleButton.h"
#include "Custom/LabelButton.h"
#include "Debug.h"
#include "Model/DataCenter.h"

using namespace model;

GroupSessionDetailDialog::GroupSessionDetailDialog(
    QWidget* parent, const QIcon& icon, const QString& id, const QString& name)
    : QDialog(parent), group_avatar(icon), group_id(id), group_name(name)
{
    this->setWindowTitle("群聊信息");
    this->setFixedSize(400, 500);
    this->setWindowIcon(QIcon(":/image/logo.png"));
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setStyleSheet(
        "QWidget {"
        "background-color: #F2F2F2;"
        "border:none;"
        "}");
    /* 初始化 title */
    QWidget*     title_wigdet = new QWidget();
    QGridLayout* g_layout     = new QGridLayout();
    title_wigdet->setStyleSheet("background-color: #FFFFFF");
    g_layout->setContentsMargins(10, 0, 0, 0);
    g_layout->setHorizontalSpacing(10);
    g_layout->setVerticalSpacing(0);
    title_wigdet->setLayout(g_layout);
    const static int LABEL_HEIGHT = 30;
    const QSize      avatar_sz    = QSize(LABEL_HEIGHT * 2, LABEL_HEIGHT * 2);

    CircleButton* avatar          = new CircleButton();
    avatar->setIcon(group_avatar);
    avatar->setIconSize(avatar_sz);
    avatar->setFixedSize(avatar_sz);

    QLabel* txt_label = new QLabel();
    txt_label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    txt_label->setFixedHeight(LABEL_HEIGHT);
    txt_label->setText(group_name);
    txt_label->setStyleSheet(
        "QLabel {"
        "font-size: 18px;"
        "font-weight: 700;"
        "}");
    QLabel* num_label = new QLabel();
    num_label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    num_label->setFixedHeight(LABEL_HEIGHT);
    num_label->setText(group_id);
    num_label->setStyleSheet(
        "QLabel {"
        "font-size: 14px;"
        "}");
    g_layout->addWidget(avatar, 0, 0, 2, 1, Qt::AlignLeft);
    g_layout->addWidget(txt_label, 0, 1, 1, 4, Qt::AlignLeft | Qt::AlignBottom);
    g_layout->addWidget(num_label, 1, 1, 1, 4, Qt::AlignLeft | Qt::AlignTop);
    /* 设置滚动区域 */
    QScrollArea* s_area = new QScrollArea();
    s_area->setStyleSheet(SCROLL_STYLE);
    s_area->setFixedSize(360, 300);
    s_area->setWidgetResizable(true);
    s_area->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    QWidget* contaienr = new QWidget();
    contaienr->setStyleSheet(
        "QWidget{background-color: "
        "#FFFFFF;"
        "}");
    s_area->setWidget(contaienr);
    g_layout_scroll = new QGridLayout();
    g_layout_scroll->setContentsMargins(10, 0, 0, 0);
    g_layout_scroll->setSpacing(0);
    contaienr->setLayout(g_layout_scroll);
    const QIcon  add_icon = QIcon(":/image/cross.png");
    LabelButton* add_btn  = new LabelButton(add_icon, "添加");
    g_layout_scroll->addWidget(add_btn, 0, 0, 1, 1, Qt::AlignLeft | Qt::AlignTop);
    /* 退出按钮 */
    QPushButton* quit_btn = new QPushButton();
    quit_btn->setFixedSize(QSize(280, 40));
    quit_btn->setText("退出群聊");
    quit_btn->setStyleSheet(
        "QPushButton{"
        "border-radius: 5px;"
        "background-color: #FFFFFF;"
        "color:#E7421F;"
        "}"
        "QPushButton:pressed {"
        "color: #FDA597;"
        "}");
    /* 添加测试数据 */
#ifdef TEST_UI
    for (int i = 1; i <= 30; ++i) {
        const QIcon   icon_m = QIcon(":/image/vanilla2.png");
        const QString name_m = QString("香草") + QString::number(i);
        add_item(icon_m, name_m);
    }
#endif

    /* 从网络中拿数据 */
    DataCenter* data_center = DataCenter::get_instance();
    connect(data_center, &DataCenter::get_group_member_list_done, this, [=] {
        QList<UserInfo> user_infos = data_center->get_group_member_list(id);
        if (user_infos.size() <= 0) {
            LOG() << "user_infos too small!";
            return;
        }
        for (const auto& user_info : user_infos) {
            add_item(user_info.avatar, user_info.nick_name);
        }
    });
    data_center->get_group_member_list_async(id);
    /* 设置布局 */
    v_layout = new QVBoxLayout();
    v_layout->setContentsMargins(20, 20, 20, 20);
    v_layout->setSpacing(20);
    this->setLayout(v_layout);
    v_layout->addWidget(title_wigdet);
    v_layout->addWidget(s_area);
    v_layout->addWidget(quit_btn, 0, Qt::AlignHCenter);
}

/// @brief 向网格布局中添加元素
void GroupSessionDetailDialog::add_item(const QIcon& icon, const QString& name)
{
    const static int MAX_COL_NUM = 4;  // 规定一行最多有4个元素
    if (cur_col >= 4) {
        cur_col = 0;
        cur_row++;
    }
    LabelButton* btn = new LabelButton(icon, name);
    g_layout_scroll->addWidget(btn, cur_row, cur_col, 1, 1, Qt::AlignTop | Qt::AlignLeft);
    cur_col++;
}
